<?php
/**
 * 大赛评委
 */
namespace Beerus\Modules\DesignerCompetition2021\Controllers;

use App\Http\Controllers\Controller;
use App\Pockets\GridDataModel as pGridDataModel;
use Doraemon\model\DesignerCompetition2021\audit\DcAuditFirstResult;
use Doraemon\model\Member;
use Doraemon\model\DesignerCompetition2021\audit\admin\DcAuditFirstWorkAdmin;
use Doraemon\model\DesignerCompetition2021\audit\DcAuditReviewer;
use Doraemon\service\DesignerCompetition2021\Audit;
use Doraemon\service\DesignerCompetition2021\DcAdmin;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;
use Encore\Admin\Widgets\Form;
use Encore\Admin\Widgets\Tab;
use Illuminate\Support\MessageBag;

class ReviewerController extends Controller {

    public function index(Request $request, Content $content) {
        $request = $request->all();
        $condition = [];
        if (!empty($request['reviewer_uid'])) {
            $condition['reviewer_uid'] = $request['reviewer_uid'];
        }
        if (!empty($request['competition_area'])) {
            $condition['competition_area'] = $request['competition_area'];
        }
        if (!empty($request['audit_step'])) {
            $condition['audit_step'] = $request['audit_step'];
        }
        $total = DcAuditReviewer::count($condition);
        $condition['ORDER'] = ['id' => 'DESC'];
        $list = DcAuditReviewer::select('*', $condition);
        $gridModel = new pGridDataModel($list, $total);
        $gridList = Admin::grid(
            $gridModel,
            function (Grid $grid) use ($gridModel) {
                $grid->disableExport();
                $grid->disableRowSelector();
                $grid->filter(function (Grid\Filter $filter) {
                    $filter->disableIdFilter();
                    $filter->column(4, function ($filter) {
                        $filter->equal('reviewer_uid', '评审员UID')->placeholder('请输入uid');
                    });
                    $filter->column(4, function ($filter) {
                        $filter->equal('audit_step', '评审阶段')->select(DcAdmin::AUDIT_STEP_TXT);
                    });
                    $filter->column(4, function ($filter) {
                        $filter->equal('competition_area', '赛区')->select(DcAdmin::COMPETITION_AREA_NAME);
                    });
                    $filter->expand();
                });

                $grid->column('id', 'ID');
                $grid->column('reviewer_uid', '评审员UID');
                $grid->column('reviewer_nick', '评审员昵称')->display(function() {
                    return Member::getNickByUid($this->reviewer_uid) ?? '';
                });
                $grid->column('audit_step', '评审阶段')->display(function() {
                    return DcAdmin::AUDIT_STEP_TXT[$this->audit_step];
                });
                $grid->column('competition_area', '赛区')->display(function() {
                    return DcAdmin::COMPETITION_AREA_NAME[$this->competition_area];
                });
                $grid->column('award_finish_audit_status', '奖项评选状态')->width(300)->display(function () {
                    $awardFinishAuditStatus = [];
                    $awardFinishAuditStatus[] = '户型奖：' . DcAdmin::FINISH_AUDIT_TXT[$this->house_audit_status];
                    $awardFinishAuditStatus[] = '平面奖：' . DcAdmin::FINISH_AUDIT_TXT[$this->plane_audit_status];
                    $awardFinishAuditStatus[] = '软装奖：' . DcAdmin::FINISH_AUDIT_TXT[$this->soft_audit_status];
                    $awardFinishAuditStatus[] = '材料/工艺奖：' . DcAdmin::FINISH_AUDIT_TXT[$this->make_audit_status];
                    $awardFinishAuditStatus[] = '生活解决奖：' . DcAdmin::FINISH_AUDIT_TXT[$this->life_audit_status];
                    return implode('<br/>', $awardFinishAuditStatus);
                });
                $grid->actions(function (Grid\Displayers\Actions $actions) {
                    $actions->disableEdit();
                });
            }
        );
        return $content
            ->header('大赛评审列表')
            ->row($gridList);
    }

    public function edit(Request $request, Content $content, $id = 0) {
        $data = DcAuditReviewer::get('*', ['id' => $id]);
        $form = new Form($data);
        if (!empty($id)) {
            $form->text('id', 'ID')->readOnly();
        }
        $form->text('reviewer_uid', '评审员UID')->required();
        $form->select('audit_step', '评审阶段')
            ->options(DcAdmin::AUDIT_STEP_TXT)
            ->default(DcAdmin::AUDIT_STEP_FIRST)
            ->required();
        $form->select('competition_area', '参赛地区')
            ->options(DcAdmin::COMPETITION_AREA_NAME)
            ->required();
        $form->confirm('确定提交吗？');
        $form->action('/admin/DesignerCompetition2021/reviewer/save');

        $header = '信息修改';
        $tab = new Tab();
        $tab->add($header, $form->render());
        return $content->header($header)
            ->breadcrumb(
                ['text' => '评审员列表'],
                ['text' => '编辑评审员信息'],
            )->row($tab);
    }

    public function save(Request $request) {
        $reviewerUid = $request->input('reviewer_uid');
        $competitionArea = $request->input('competition_area');
        $auditStep = $request->input('audit_step');
        // 评审员uid是否存在判断
        if (!Member::getUserInfo($reviewerUid)) {
            $error = new MessageBag(['title' => '该评审员不存在']);
            return back()->with(compact('error'));
        }
        if (!array_key_exists($competitionArea, DcAdmin::COMPETITION_AREA_NAME)) {
            $error = new MessageBag(['title' => '该赛区不存在']);
            return back()->with(compact('error'));
        }
        if (!in_array($auditStep, DcAdmin::AUDIT_STEP_INDEX)) {
            $error = new MessageBag(['title' => '该评审阶段不存在']);
            return back()->with(compact('error'));
        }
        $data = [
            'reviewer_uid' => $reviewerUid,
            'competition_area' => $competitionArea,
            'audit_step' => $auditStep
        ];
        if (DcAuditReviewer::get('*', ['reviewer_uid' => $reviewerUid, 'audit_step' => $auditStep, 'competition_area' => $competitionArea])) {
            $error = new MessageBag(['title' => '该评审已存在']);
            return back()->with(compact('error'));
        }
        if (!DcAuditReviewer::addInfo($data)) {
            $error = new MessageBag(['title' => '新增评审失败']);
            return back()->with(compact('error'));
        }
        $success = new MessageBag(['title' => '操作成功']);
        return redirect('/admin/DesignerCompetition2021/reviewer')->with(compact('success'));
    }

    // 删除奖项
    public function del($id) {
        $reviewer = DcAuditReviewer::get('*', ['id' => $id]);
        if ($reviewer) {
            if (DcAuditReviewer::delete(['id' => $id]) === false) {
                return response()->json([
                    'status' => false,
                    'message' => "操作失败",
                ]);
            }
            Audit::initActionAndGetCompetitionArea($reviewer['audit_step']);
            Audit::$dcAuditResultClass::delete([
                'reviewer_uid' => $reviewer['reviewer_uid'],
                'competition_area' => $reviewer['competition_area']
            ]);
        } else {
            return response()->json([
                'status' => false,
                'message' => "该评审不存在",
            ]);
        }
    }
}
